package io.renren.common.utils;

import cn.hutool.core.util.NumberUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * BigDecimal类型返回前端保留两位小数
 *
 * @author 40659
 */
public class BigDecimalSerialize extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (null != bigDecimal) {
            jsonGenerator.writeString(NumberUtil.round(bigDecimal, 2).toPlainString());
        } else {
            jsonGenerator.writeString(BigDecimal.ZERO.toPlainString());
        }
    }
}
